local ROT = require 'rot'

local Line = ROT.Class:extend()

function Line:init(toX, toY, passable, opt)
    self.toX, self.toY, self.passable, self.opt = toX, toY, passable, opt or {}
end

function Line:compute(fromX, fromY, callback)
    if self.passable(fromX, fromY) then callback(fromX, fromY) else return end

    local vx, vy = self.toX - fromX, self.toY - fromY
    local dist = math.max(math.abs(vx), math.abs(vy))

    for i = 1, self.opt.distance or dist do
        local c = i / dist
        local x = math.floor(fromX + 0.5 + vx * c)
        local y = math.floor(fromY + 0.5 + vy * c)
        if self.passable(x, y) then callback(x, y) else return end
    end
end

return Line
